<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of UtilController
 *
 * @author thanhlnh
 */
class UtilController extends Zend_Controller_Action {

    public function indexAction() {
        $this->_redirect('util/list');
    }

    public function listAction() {
        
    }

    function getInnerHTML($Node) {
        $Document = new DOMDocument();
        $Document->appendChild($Document->importNode($Node, true));
        return $Document->saveHTML();
    }

    public function ngoaiteAction() {
        $this->_helper->viewRenderer->setNoRender(true);
        $client = new Zend_Http_Client();
        $client->setUri("http://hcm.24h.com.vn/ttcb/tygia/tygia.php");
        $response = $client->request();
        $html = $response->getBody();
        $dom = new Zend_Dom_Query($html);
        $results = $dom->query('.columnCenter');

        $content = '';
        $results->rewind();
        while ($results->valid()) {
            $domDoc = $results->current();

            $table = $domDoc->getElementsByTagName("table");
            if ($table->item(0)) {
                $content = $this->getInnerHtml($table->item(0));
                break;
            }
            $results->next();
        }
        echo ' <link rel="stylesheet" type="text/css" href="http://www.24h.com.vn/ttcb/tygia/css/style.css">';
        $content = str_replace('src="images/', 'src="http://hcm.24h.com.vn/ttcb/tygia/images/', $content);
        echo $content;
    }

    public function xosoAction() {
        $this->_helper->viewRenderer->setNoRender(true);
        $client = new Zend_Http_Client();
        $client->setUri("http://ketquaxoso.24h.com.vn/");
        $response = $client->request();
        $html = $response->getBody();
        $dom = new Zend_Dom_Query($html);
        $results = $dom->query('table');

        $content = '';
        $results->rewind();
        $index = 0;
        while ($results->valid()) {
            $domDoc = $results->current();
            $table = $domDoc->getElementsByTagName("table");
            if ($table->item(0)) {
                $tr = $table->item(0)->getElementsByTagName("tr");
                if ($tr->length >= 4) {
                    $index++;
                    $content .= $this->getInnerHtml($domDoc);
                }
            }
            if ($index == 7)
                break;
            $results->next();
        }

        echo '<link href="http://ketquaxoso.24h.com.vn/css/xoso_style.css" rel="stylesheet" type="text/css" />';
        echo $content;
    }

    public function goldAction() {
        $this->_helper->viewRenderer->setNoRender(true);
        $client = new Zend_Http_Client();
        $client->setUri("http://hcm.24h.com.vn/ttcb/giavang/giavang.php");
        $response = $client->request();
        $html = $response->getBody();
        $dom = new Zend_Dom_Query($html);
        $results = $dom->query('.columnCenter');

        $content = '';
        $results->rewind();
        while ($results->valid()) {
            $domDoc = $results->current();

            $table = $domDoc->getElementsByTagName("table");
            if ($table->item(0)) {
                $content = $this->getInnerHtml($table->item(0));
                break;
            }
            $results->next();
        }
        echo '<link rel="stylesheet" type="text/css" href="http://www.24h.com.vn/ttcb/giavang/css/style.css">';
        $content = str_replace('src="images/', 'src="http://hcm.24h.com.vn/ttcb/giavang/images/', $content);
        echo $content;
    }

    public function weatherAction() {
        $this->_helper->viewRenderer->setNoRender(true);
        $client = new Zend_Http_Client();
        $client->setUri("http://hcm.24h.com.vn/ttcb/thoitiet/thoitiet.php");
        $response = $client->request();
        $html = $response->getBody();
        $dom = new Zend_Dom_Query($html);
        $results = $dom->query('.columnCenter');

        $content = '';
        $results->rewind();
        while ($results->valid()) {
            $domDoc = $results->current();

            $table = $domDoc->getElementsByTagName("table");
            if ($table->item(0)) {
                $content = $this->getInnerHtml($table->item(0));
                break;
            }
            $results->next();
        }
        echo ' <link rel="stylesheet" type="text/css" href="http://www.24h.com.vn/ttcb/thoitiet/css/style.css">';
        $content = str_replace('src="images/', 'src="http://hcm.24h.com.vn/ttcb/thoitiet/images/', $content);
        echo $content;
    }

    public function tiviAction() {
        $this->_helper->viewRenderer->setNoRender(true);
        $client = new Zend_Http_Client();
        $client->setUri("http://hcm.24h.com.vn/ttcb/truyenhinh/truyenhinh.php");
        $response = $client->request();
        $html = $response->getBody();
        $dom = new Zend_Dom_Query($html);
        $results = $dom->query('table');

        $content = '';
        $results->rewind();
        while ($results->valid()) {
            $domDoc = $results->current();
            if ($domDoc->getAttribute('style') == "border-collapse: collapse"
                    && $domDoc->getAttribute('align') == "left") {
                $tr = $domDoc->getElementsByTagName('td')->item(0)->getElementsByTagName('tr')->item(1);
                $tr = $tr->getElementsByTagName('table')->item(0);
                $content = $this->getInnerHtml($tr);
                break;
            }
            $results->next();
        }
        echo ' <link rel="stylesheet" type="text/css" href="http://www.24h.com.vn/ttcb/thoitiet/css/style.css">';
        echo $content;
    }

    public function footballAction() {
        $this->_helper->viewRenderer->setNoRender(true);
        $client = new Zend_Http_Client();
        $client->setUri("http://bongda.wap.vn/");
        $response = $client->request();
        echo $response->getBody();
    }

}

?>
